package com.xxhui.spidermapdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.xxhui.spidermapdemo.bean.SpiderDate;
import com.xxhui.spidermapdemo.bean.SpiderPart;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hui on 2016/6/17.
 */
public class SpiderMap extends View {

    private static final String TAG = "SpiderMap" ;
    private int cur_width;
    private int cur_height;
    private SpiderPart part = new SpiderPart();
    Paint paint = new Paint();

    Paint datePaint = new Paint();

    private int down_x;

    private int down_y;

    private int move_to_lest_local_x;

    private int move_to_lest_local_y;

    private int autoDataCount = 6;

    private List<SpiderDate> list ;


    public SpiderMap(Context context) {
        super(context);
    }

    public SpiderMap(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cur_height = h;
        cur_width = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        initSpiderPart(autoDataCount);
        if(part==null){
            Toast.makeText(getContext() , "null" , Toast.LENGTH_SHORT).show();
            return;
        }
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        canvas.translate(cur_width / 2, cur_height / 2);
        canvas.drawARGB(98, 98, 98, 98);
        canvas.drawPoint(0 , 0 , paint);
        int size  = part.getSize();
        for(int i=0 ; i<size ;i++){
            for(int j=1 ; j<=5 ; j++) {//这个循环画5条圆心正下方的横线
                canvas.drawLine(-part.getHalf_buttom_side()*j, part.getRise_height()*j, part.getHalf_buttom_side()*j, part.getRise_height()*j, paint);
            }
            canvas.drawLine(0, 0, part.getHalf_buttom_side()*5, part.getRise_height()*5, paint);//画一条线，在5条线的右边顶点相接
            canvas.drawPoint( part.getHalf_buttom_side()*5, part.getRise_height()*5, paint);//在顶点画一个点，因为画出来会有个分叉，覆盖一个点就看不出来了
            canvas.rotate(part.getRoteteAngle());
        }
       drawDate(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down_x = (int) event.getRawX();
                down_y = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                move_to_lest_local_x = (int) event.getRawX();
                move_to_lest_local_y = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                if (down_x - move_to_lest_local_x > 0) {
                    this.setSize(autoDataCount--);
                } else {
                    this.setSize(autoDataCount++);
                }
                if (autoDataCount <= 0 ) {
                    Toast.makeText(getContext(), "大哥你想干嘛,饼图都要没了(＞﹏＜)", Toast.LENGTH_SHORT).show();
                    autoDataCount = 0;
                    break;
                }
                //Toast.makeText(getContext(), String.valueOf(autoDataCount), Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return true;
    }

    public void setSize(int size){
        initSpiderPart(size);
        setAutoList(autoDataCount);
        this.invalidate();//刷新画布，实际上是重新调用ondraw方法
    }

    private void initSpiderPart(int size){
        if(size<3){
            Toast.makeText(getContext() , "三角形是最小的了耶！" , Toast.LENGTH_SHORT ).show();
            autoDataCount = 3 ;
            size = 3 ;
        }
        part.setSize(size);
        part.setRoteteAngle(360.0f/size);//设置旋转角
        int min = Math.min(cur_width ,cur_height);
        part.setBevel_edge(min*0.8f/10);
        double buttom_angle = (double) (90-part.getRoteteAngle()/2);//底边角度，相当于（180-RoteteAngle）/2
        double angleRadians = buttom_angle* Math.PI/180;//转换为弧度角
        float rise_height = (float)Math.sin(angleRadians)*part.getBevel_edge();
        part.setRise_height(rise_height);
        float half_buttom_side = (float) Math.cos(angleRadians)*part.getBevel_edge();
        part.setHalf_buttom_side(half_buttom_side);

    }

    //------------------------------------------------------上面是画蜘蛛图的代码

    //------------------------------------------------------下面是画数据的代码

    public void setList(List<SpiderDate> list) {
        this.list = list;
    }

    public void setAutoList(int size){
        if(size<3){
            size=3;
        }
        this.list = getAutoDate(size);
        handleList(this.list);
    }

    private List<SpiderDate> getAutoDate(int size){
        List<SpiderDate> list = new ArrayList<SpiderDate>();
        for(int i=0 ;i < size ; i++){
            SpiderDate date = new SpiderDate();
            date.setName(String.valueOf(i));
            date.setValue((int) (Math.random()*101));
            list.add(date);
        }
        return  list;
    }

    private void handleList(List<SpiderDate> list){
        if(list==null||list.size()==0){
            return;
        }
        float sum=100;
        SpiderDate date = null;
        int size = list.size();
//        for (int i=0 ; i<size ; i++){
//            sum+=list.get(i).getValue();
//        }
        for(int j=0 ; j<size ;j++){
            date = list.get(j);
            date.setPercentage(date.getValue()/sum);
        }
        Toast.makeText(getContext(), "list:"+list.size(), Toast.LENGTH_SHORT).show();
    }

    private void drawDate(Canvas canvas){
        if(list==null||list.size()==0) {
            Toast.makeText(getContext(), "list null", Toast.LENGTH_SHORT).show();
            return;
        }
        float radius = part.getBevel_edge()*5;//半径
        double startAngle = 90+part.getRoteteAngle()/2;//起始角度(从平面直角坐标系的X的正半轴做起始轴，加上度数向顺时针旋转，减去度数，逆时针旋转)
        double angleRadians = startAngle* Math.PI/180;//转换为弧度角

        paint.setStrokeWidth(7);
        paint.setColor(Color.BLUE);
        paint.setAlpha(189);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        int size  = part.getSize();//正size边形
        datePaint.setColor(Color.RED);
        datePaint.setAlpha(89);
        datePaint.setStrokeWidth(5);
        datePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        Path path = new Path();
        for(int j=0 ; j<size ; j++){
            SpiderDate date =  list.get(j);
            float x = (float) (radius* Math.cos(angleRadians))*date.getPercentage();//极坐标表示x = ρcosθ*百分比，其中ρcosθ表示该角度顶点的x坐标，乘以百分比就是要求的坐标
                    //part.getHalf_buttom_side()*5*date.getPercentage();
            float y = (float) (radius* Math.sin(angleRadians))*date.getPercentage();//极坐标表示y = ρsinθ*百分比，其中ρsinθ表示该角度顶点的y坐标，乘以百分比就是要求的坐标
                    //part.getRise_height()*5*date.getPercentage();
            if(j==0) {
                path.moveTo(x, y);
            }else {
                path.lineTo(x ,y);
            }
            //canvas.drawCircle(x,y,10,paint);
            startAngle=(startAngle+part.getRoteteAngle())%360;
            angleRadians = startAngle* Math.PI/180;//转换为弧度角
        }
        path.close();
        canvas.drawPath(path , datePaint);

        for(int j=0 ; j<size ; j++){
            SpiderDate date =  list.get(j);
            float x = (float) (radius* Math.cos(angleRadians))*date.getPercentage();
            //part.getHalf_buttom_side()*5*date.getPercentage();
            float y = (float) (radius* Math.sin(angleRadians))*date.getPercentage();
            //part.getRise_height()*5*date.getPercentage();
            if(j==0) {
                path.moveTo(x, y);
            }else {
                path.lineTo(x ,y);
            }
            canvas.drawCircle(x,y,10,paint);
            startAngle=(startAngle+part.getRoteteAngle())%360;
            angleRadians = startAngle* Math.PI/180;//转换为弧度角
        }
    }
}
